//
//  ZJWebAutoTool.m
//  Weather
//
//  Created by 余志杰 on 17/7/4.
//  Copyright © 2017年 zhenhui huang. All rights reserved.
//

#import "ZJWebAutoTool.h"
#import "ZJWebTool.h"
#import "ZJConstant.h"
#import <objc/runtime.h>
#import "AppDelegate.h"
#import "UMessage.h"

@class ZJWebAutoTool;
static ZJWebAutoTool *kWebAutoTool;

static inline void zj_swizzleSelector(Class originalClass, SEL originalSelector,Class swizzledClass, SEL swizzledSelector)
{
	Method originalMethod = class_getInstanceMethod(originalClass, originalSelector);
	Method swizzledMethod = class_getInstanceMethod(swizzledClass, swizzledSelector);
	method_exchangeImplementations(originalMethod, swizzledMethod);
}

@interface AppDelegate (private)

@end

@implementation AppDelegate (private)

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{}
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{}

@end

@interface ZJWebAutoTool ()<UNUserNotificationCenterDelegate>

@end

@implementation ZJWebAutoTool

+ (void)load
{
	if(self == [ZJWebAutoTool class])
	{
		zj_swizzleSelector([AppDelegate class],@selector(application:didFinishLaunchingWithOptions:),self,@selector(application:didFinishLaunchingWithOptions:));
		zj_swizzleSelector([AppDelegate class],@selector(application:didReceiveRemoteNotification:),self,@selector(application:didReceiveRemoteNotification:));
		zj_swizzleSelector([AppDelegate class],@selector(userNotificationCenter:willPresentNotification:withCompletionHandler:),self,@selector(userNotificationCenter:willPresentNotification:withCompletionHandler:));
		zj_swizzleSelector([AppDelegate class],@selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:),self,@selector(userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:));

		
		kWebAutoTool = [[ZJWebAutoTool alloc] init];
	}
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
	[UMessage startWithAppkey:kUMAppKey launchOptions:launchOptions];
	[UMessage setLogEnabled:YES];
	[UMessage registerForRemoteNotifications];
	
	//iOS10必须加下面这段代码。
	UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
	center.delegate= self;
	UNAuthorizationOptions authorizations = UNAuthorizationOptionBadge|  UNAuthorizationOptionAlert|UNAuthorizationOptionSound;
	[center requestAuthorizationWithOptions:authorizations completionHandler:^(BOOL granted, NSError * _Nullable error) {
		if (granted) {
			//点击允许
			//这里可以添加一些自己的逻辑
		} else {
			//点击不允许
			//这里可以添加一些自己的逻辑
		}
	}];
		
		
	 [ZJWebTool startWithKey:kBombKey tableName:kTableName appName:kAppName];
	return [kWebAutoTool application:application didFinishLaunchingWithOptions:launchOptions];
}

	 
 //iOS10以下使用这个方法接收通知
 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
	[UMessage didReceiveRemoteNotification:userInfo];
}
	 
	 //iOS10新增：处理前台收到通知的代理方法
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
	 NSDictionary * userInfo = notification.request.content.userInfo;
	 if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]])
	 {
		 //应用处于前台时的远程推送接受
		 //关闭U-Push自带的弹出框
		 [UMessage setAutoAlert:NO];
		 //必须加这句代码
		 [UMessage didReceiveRemoteNotification:userInfo];
		 
	 }else{
		 //应用处于前台时的本地推送接受
	 }
	 //当应用处于前台时提示设置，需要哪个可以设置哪一个
	 completionHandler(UNNotificationPresentationOptionSound|UNNotificationPresentationOptionBadge|UNNotificationPresentationOptionAlert);
 }
	 
 //iOS10新增：处理后台点击通知的代理方法
 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler
{
	 NSDictionary * userInfo = response.notification.request.content.userInfo;
	 if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
		 //应用处于后台时的远程推送接受
		 //必须加这句代码
		 [UMessage didReceiveRemoteNotification:userInfo];
		 
	 }else{
		 //应用处于后台时的本地推送接受
	 }
 }

@end
